<?php
/*
Acan
*/
//禁用错误报告
//error_reporting(0);
//报告所有错误
/*
private只有父类自己才能使用
protected子类和父类才可以使用
数组转对象	$Object = new ArrayObject(array());
对象转数组	$arr = (array)$Object;

*/
//error_reporting(E_ALL);
//----Acan

class Acan{
	
	function __construct(){
		global $_G;
		$this->_G = $_G;
		$this->aurl();
		$this->_SS = Afun_session();
	}
	
	//初始化
	function arun(){
		$this->Ainfo();
		define('Acan_apptpl_path',$_GET['app'].'/');
		$acan_path = A_ROOT.'app/'.$_GET['app'].'/Con_'.$_GET['mod'].'.php';
		if(!file_exists($acan_path)){
			$acan_path = A_ROOT.'app/public/Con_'.$_GET['mod'].'.php';
		}
		if(!file_exists($acan_path)){
			Afun_error('未定义的类:Con_'.$_GET['mod'].'<br>'.'/app/'.$_GET['app'].'/Con_'.$_GET['mod'].'.php');
		}
		include($acan_path);
		$newname = 'Con_'.$_GET['mod'];
		$Acan = new $newname;
		if(empty($_GET['act'])){
			$_GET['act'] = $Acan->defaultAct;
		}
		$this->auth();//认证
		$Acan_methods = get_class_methods(get_class($Acan));
		$act = 'Act_'.$_GET['act'];
		if(!in_array($act,$Acan_methods)){
			Afun_error('未定义的方法:'.$newname.':'.$act.'()<br>'.'文件:/app/'.$_GET['app'].'/Con_'.$_GET['mod'].'.php');
		}
		$Acan->$act();
	}
	//权限判断
	function auth(){
		$_SS = Afun_session();
		if($this->Aapp=='tool' || $this->Aapp=='auth'){return;}
		if($this->Aapp=='danhao'){
			if($this->Amod=='data'){return;}
			if($this->Amod=='news'){
				if($this->Aact=='show'){return;}
			}
			if($this->Amod=='index'){return;}
			if($this->Amod=='help'){return;}
			if($this->Amod=='user'){
				if(in_array($this->Aact,array('reg_rand','reg','reg_go'))){return;}
			}
			if($this->Amod=='cash'){
				if($this->Aact=='call'){return;}
			}
			if($this->Amod=='exp'){
				if($this->Aact=='list'){return;}
				else if($this->Aact==''){return;}
				else if($this->Aact=='post'){
					if(in_array(Afun_get_ip(),array('112.124.9.158','127.0.0.1'))){return;}
				}
			}
		}
		if($this->Aapp=='asys'){
			if($this->Amod=='alogs'){return;}
			if($this->Amod=='2dfire'){
				if($this->Aact=='stat'){return;}
			}
		}
		if($this->Aapp=='weibo'){
			if($this->Amod=='tool'){return;}
			if($this->Amod=='api'){return;}
			if($this->Amod=='index'){return;}
			if($this->Amod=='mobile'){return;}
			if($this->Amod=='web'){return;}
		}
		if($this->Amod == 'login'){return;}
		//--判断是否登入
		if(empty($_SS['uid'])){
			$loginr = 1;
			include $this->Atpl('tpl/login');
			exit;
		}else{
			if($_SS['uid']==1){return;}
			Afunc_group()->auth();
		}
		
		/*
		if($_POST){
			if($_SS['uid'] == '1'){return;}//限制操作
			Afun_refresh("你不是管理员，不能执行此操作",'referrer');
			exit;
		}*/
	}
	//url
	function aurl(){
		$this->Amobile = '';
		$this->Amobile_status = $this->checkmobile();
		$this->base_domain = $_SERVER["SERVER_NAME"];
		if(defined('Acan_base_domain')){
			$this->base_domain = Acan_base_domain;
		}
		$this->domain = '';
		if(defined('Acan_app_domain')){
			$this->domain = Acan_app_domain.'.'.$this->base_domain;
			$this->siteurl = 'http://'.$this->domain;
		}else{
			$this->siteurl = 'http://'.$_SERVER["HTTP_HOST"];
		}
		$_GET['app'] = Acan_app_name;
		$Urr = explode('-',$_GET['i']);
		$_GET['mod'] = $Urr[0];
		$_GET['act'] = $Urr[1];
		$_GET['id'] = $Urr[2];
		$Ai = 3;
		$this->Aapp = $_GET['app'];//入口文件
		$this->Amod = $_GET['mod'];//con
		$this->Amode = '0';
		if(empty($_GET['mod'])){$_GET['mod'] = 'index';}
		$Unum = count($Urr);
		if($Unum>$Ai){
			for ($i=$Ai; $i < $Unum; $i=$i+2) {
				$_GET[$Urr[$i]] = $Urr[$i+1];
			}
		}
		if(empty($this->Aapp)){$this->Aapp = $_GET['app'];}
		if(empty($this->Amod)){$this->Amod = $_GET['mod'];}
		$this->id = $_GET['id'];
		$this->Aact = $_GET['act'];//act
		if(empty($this->Aact))//act 路径
			$this->Aact = $this->defaultAct;
		if(empty($this->Amod)){
			$this->Amodr = '';//con 路径
		}elseif(empty($this->Aapp)){
			$this->Amodr = $this->Amod;//con 路径
		}else{
			$this->Amodr = $this->Aapp.'-'.$this->Amod;//con 路径
		}
		if(!empty($this->domain)){
			$this->Amodr = $this->siteurl.str_replace($this->Aapp.'-','/',$this->Amodr);
			$this->Aappr = $this->siteurl.'/';
		}
		$this->Aactr = $this->Amodr.'-'.$this->Aact;
	}
	//url
	function aurl2(){
		$_GET['app'] = Acan_app_name;
		$Aaurl = str_replace('.'.Acan_site_domain,'',$_SERVER['HTTP_HOST']);
		$Arr = explode('-',$Aaurl);
		$hrr = explode('.',$_SERVER['HTTP_HOST']);
		if(count($hrr)==1 || preg_match("/([a-zA-Z])/i",$_SERVER['HTTP_HOST'])==0){unset($Arr);}//排除非正常域名
		if(!in_array($Aaurl,$this->Allow_domain())){unset($Arr);}//排除未知域名
		$Urr = explode('-',$_GET['i']);
		if(count($Arr)==1 && $Arr[0] != Acan_site_domain && $Arr[0] != 'www'){
			$_GET['mod'] = $Urr[0];
			$_GET['act'] = $Urr[1];
			$_GET['id'] = $Urr[2];
			$_GET['Aurl'] = '';
			$Ai = 3;
			$this->Amod = $_GET['mod'];//con
			$this->Amode = '1';
		}elseif(count($Arr)==2){
			$_GET['mod'] = $Arr[0];
			$_GET['act'] = $Urr[0];
			$_GET['id'] = $Urr[1];
			$Ai = 2;
			$this->Amode = '2';
		}else{
			$_GET['mod'] = $Urr[1];
			$_GET['act'] = $Urr[2];
			$_GET['id'] = $Urr[3];
			$Ai = 4;
			$this->Aapp = $_GET['app'];//入口文件
			$this->Amod = $_GET['mod'];//con
			$this->Amode = '0';
		}
		if(empty($_GET['mod'])){$_GET['mod'] = 'index';}
		if(empty($_GET['act'])){$_GET['act'] = 'index';}
		$Unum = count($Urr);
		if($Unum>$Ai){
			for ($i=$Ai; $i < $Unum; $i=$i+2) {
				$_GET[$Urr[$i]] = $Urr[$i+1];
			}
		}
		$this->id = $_GET['id'];
		$this->Aact = $_GET['act'];//act
		if(empty($this->Amod)){
			$this->Amodr = '';//con 路径
		}elseif(empty($this->Aapp)){
			$this->Amodr = $this->Amod;//con 路径
		}else{
			$this->Amodr = $this->Aapp.'-'.$this->Amod;//con 路径
		}
		if($this->Aact)//act 路径
			$this->Aactr = $this->Amodr.'-'.$this->Aact;
		else
			$this->Aactr = $this->Amodr.'-'.$this->defaultAct;
		if(empty($this->Aapp)){$this->Aapp = $_GET['app'];}
		if(empty($this->Amod)){$this->Amod = $_GET['mod'];}
	}
	//模板引擎
	function Atpl($tplpath='',$type=''){
		if(empty($tplpath)){
			if(empty($_GET['act'])){
				$_GET['act'] = $this->defaultAct;
			}
			$tplpath = $_GET['mod'].'/'.$_GET['act'];
		}
		return Afun_template($tplpath,'1',$type);
	}
	//载入模板
	function Atpls($data){
		foreach($data as $key=>$val){
			$$key = $val;
		}
		include $this->Atpl();
	}
	function Ahtml($tplpath=''){
		return $this->Atpl($tplpath,'html');
	}
	//站点信息读取
	function Ainfo(){
		$op = Afunc_option();
		define('Acan_debug',$op->get('Acan_debug'));
		define('Acan_site_email',$op->get('Acan_site_email'));		// Acan 邮箱
		define('Acan_site_domain',$op->get('Acan_site_domain'));	// Acan 站点域名
		define('Acan_site_url',$op->get('Acan_site_url'));			// Acan 网址
		define('Acan_site_name',$op->get('Acan_site_name'));		// Acan 站点名称
		define('Acan_site_icp',$op->get('Acan_site_icp'));			// Acan 备案号
		define('Acan_site_logo',$op->get('Acan_site_logo'));		// Acan logo
		define('Acan_site_qqcode',$op->get('Acan_site_qqcode'));	// Acan qqcode
		define('Acan_site_jscode',$op->get('Acan_site_jscode'));	// Acan jscode
		define('Acan_info_version','1.0');							// Acan 版本
		define('Acan_info_copyright','Powered by <a  href="http://'.Acan_site_domain.'" target="_blank"><strong>Acan</strong></a> <span>'.Acan_info_version.'</span>&copy; 2008-2012');			// Acan 版权
	}
	//获取默认数据
	function Adata(){
		$data['aurl_model'] = $this->aurl->name.'.php?a='.$this->aurl[0];
		if($this->aurl[1])
			$data['aurl_act'] = $this->aurl->name.'.php?a='.$this->aurl[0].'/'.$this->aurl[1];
		else
			$data['aurl_act'] = $this->aurl->name.'.php?a='.$this->aurl[0].'/'.$this->defaultAct;
		return $data;
	}
	function Allow_domain(){
		return array();
	}
	//mysql
	function Amysql(){
		return Afun_db();
	}
	
	function Ahtml_js_aurl(){
		return "<script>
			var Aurl = {'App':'".$this->Aapp."','Amod':'".$this->Amod."','Aact':'".$this->Aact."','Amodr':'".$this->Amodr."','Aactr':'".$this->Aactr."'};
			var Acan_app_cname='".Acan_app_cname."';
		</script>";
	}
	
	
	//程序执行时间统计
	function Aruntime_stats(){
		return '执行'.round(gettime() - Acan_run_startime, 3).'秒,使用'.Afun_memory_usage().'内存';
	}
	/*
	function __call($v, $s) {
	//	$this->ClassDetail(__CLASS__);
		$html = '<hr>';
		$html .= $_SERVER['SCRIPT_FILENAME'] .'出错';
		$html .= 'function '.$v.'() 不存在 <br>'. implode(',', $s);
		echo $html;
	}
	
	
	
	function ClassDetail($classname){
		if(!class_exists($classname)){
			echo($classname.'类不存在');
		}else{
			print_r("以下描述类".$classname);
			print_r("所有的方法<pre>");
			print_r(get_class_methods($classname));
			print_r("</pre>");
			print_r("<hr/>所有的属性<pre>");
			print_r(get_class_vars($classname));
			print_r("</pre><hr/>");
		}
	}
	
	*/
	function __destruct() {
		unset($_SESSION['mysql']);
		global $Amem;
		if(is_object($Amem)) $Amem->close();
	}
	
	function checkmobile() {
		$mobile = array();
		static $mobilebrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
					'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
					'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
					'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
					'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
					'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
					'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
		$pad_list = array('pad', 'gt-p1000');

		$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

		if($this->dstrpos($useragent, $pad_list)) {
			return false;
		}
		if(($v = $this->dstrpos($useragent, $mobilebrowser_list, true))) {
			$this->Amobile = $v;
			return true;
		}
		$brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
		if($this->dstrpos($useragent, $brower)) return false;

		$this->Amobile = 'unknown';
		if($_GET['mobile'] === 'yes') {
			return true;
		} else {
			return false;
		}
	}

	function dstrpos($string, &$arr, $returnvalue = false) {
		if(empty($string)) return false;
		foreach((array)$arr as $v) {
			if(strpos($string, $v) !== false) {
				$return = $returnvalue ? $v : true;
				return $return;
			}
		}
		return false;
	}
}







?>